package com.dlmu.HashMapDemo;

import java.util.ArrayList;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.function.Consumer;

public class HashMap {
    public static void main(String[] args) {
        //某班级80名学生，现需要秋游，秋游有四个景点{A,B,C,D}
        //每个学生只能选一个景点，统计最后哪个景点选择的人数最多

        //先让学生选景点
        String[]arr={"A","B","C","D"};
        ArrayList<String> list=new ArrayList<>();
        Random random=new Random();
        //80个学生，每个学生从4个景点中随机选择一个景点

        for (int i = 0; i < 80; i++) {
            int i1 = random.nextInt(arr.length);
            list.add(arr[i1]);
        }
        //
        java.util.HashMap<String,Integer> hm=new java.util.HashMap<>();
        //遍历集合得到每一个选项
        for(String name:list){
            if(hm.containsKey(name)){
                //如果存在该景点
                //根据键获取到对应的值
                int count=hm.get(name);
                count++;
                hm.put(name,count);
            }else{
                //不存在
                hm.put(name,1);
            }
        }
        Set<Map.Entry<String, Integer>> entries = hm.entrySet();
        entries.forEach(new Consumer<Map.Entry<String, Integer>>() {
            @Override
            public void accept(Map.Entry<String, Integer> stringIntegerEntry) {
                String name=stringIntegerEntry.getKey();
                int num=stringIntegerEntry.getValue();
                System.out.println(name+"="+num);
            }
        });
        System.out.println("-----------------------------------");
        int max=0;
        for (Map.Entry<String, Integer> entry : entries) {
            if(entry.getValue()>max){
                max=entry.getValue();
            }
        }
        System.out.println(max);
        for (Map.Entry<String, Integer> entry : entries) {
            if(entry.getValue()==max){
                System.out.println(entry.getKey());
            }
        }






    }
}
